import Vue from "vue";
import VueRouter from "vue-router";
const Login = () => import("@/components/MyLogin.vue");
const Home = () => import("@/components/MyHome.vue");
const Welcome = () => import("@/components/MyWelcome.vue");
const Users = () => import("@/components/views/MyUser.vue");
const Rights = () => import("@/components/power/MyRights.vue");
const Roles = () => import("@/components/power/MyRoles.vue");
const Cate = () => import("@/components/goods/MyCate.vue");
const Params = () => import("@/components/goods/MyParams.vue");
const Lists = () => import("@/components/goods/MyList.vue");
const Add = () => import("@/components/goods/addGoods.vue");
const Order = () => import("@/components/order/MyOrder.vue");
const Report = () => import("@/components/report/MyReport.vue");
Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    // 重定向这里还是指向路径而不是组件
    // 组件在哪里显示就时谁的子组件
    { path: "/", redirect: "/login" },
    { path: "/login", component: Login },
    {
      path: "/home",
      component: Home,
      redirect: "/welcome",
      children: [
        {
          path: "/welcome",
          component: Welcome,
        },
        {
          path: "/users",
          component: Users,
        },
        {
          path: "/rights",
          component: Rights,
        },
        {
          path: "/roles",
          component: Roles,
        },
        {
          path: "/categories",
          component: Cate,
        },
        {
          path: "/params",
          component: Params,
        },
        {
          path: "/goods",
          component: Lists,
        },
        {
          path: "/goods/add",
          component: Add,
        },
        {
          path: "/orders",
          component: Order,
        },
        {
          path: "/reports",
          component: Report,
        },
      ],
    },
  ],
});
//过载路由守卫 next函数表示放行 可以加参数表示放行到哪个页面
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  //拿到token值
  const tokenstr = window.sessionStorage.getItem("token");
  if (!tokenstr) return next("/login");
  next();
});

export default router;
